package com.example.knowman.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.example.knowman.ObService.OBKnowMainService;
import com.example.knowman.ObService.OBarchLabelMainService;
import com.example.knowman.ObService.OBknowLabelMainService;
import com.example.knowman.ReturnMessage.*;
import com.example.knowman.common.Log4Util;
import com.example.knowman.dao.knowMainEx;
import com.example.knowman.dao.knowMainExDaBiao;
import com.example.knowman.dao.labelCategoryWithStaffNAndKnowCategoryN;
import com.example.knowman.entity.archDept;
import com.example.knowman.entity.knowMain;
import com.example.knowman.entity.staffList;
import com.example.knowman.service.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//9.2发布知识
@RestController
@RequestMapping("/knowMain")
public class KnowMainController {
    private final IKnowMainService iKnowMainService;

    private final OBknowLabelMainService oBknowLabelMainService;

    private final OBarchLabelMainService oBarchLabelMainService;

    private final IStaffListService iStaffListService;

    private final IknowArchFilesService iknowArchFilesService;

    private final IArchDeptService iArchDeptService;

    private final ILog4operateService iLog4operateService;

    private final OBKnowMainService obKnowMainService;

    private final IMessageService iMessageService;

    public KnowMainController(IKnowMainService iKnowMainService,OBknowLabelMainService oBknowLabelMainService,IStaffListService iStaffListService,
                              OBarchLabelMainService oBarchLabelMainService,IknowArchFilesService iknowArchFilesService,IArchDeptService iArchDeptService,
                              ILog4operateService iLog4operateService,OBKnowMainService obKnowMainService,IMessageService iMessageService){
        this.iKnowMainService = iKnowMainService;
        this.oBknowLabelMainService = oBknowLabelMainService;
        this.iStaffListService = iStaffListService;
        this.oBarchLabelMainService = oBarchLabelMainService;
        this.iknowArchFilesService = iknowArchFilesService;
        this.iArchDeptService = iArchDeptService;
        this.iLog4operateService = iLog4operateService;
        this.obKnowMainService = obKnowMainService;
        this.iMessageService = iMessageService;
    }




    //查
    @GetMapping("getAll")
    public ResponseEntity<MExKnowMain9_2_SN_KCN> getAll(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                         @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                         @RequestParam(required = false) String knowTitle,
                                                        @RequestParam(required = false) Integer knowCateId,
                                                        @RequestParam(required = false) Integer readCount,
                                                        @RequestParam(required = false) Integer praiseCount,
                                                        @RequestParam(required = false) Integer commentCount,
                                                        @RequestParam(required = false) Integer knowState,
                                                        @RequestParam(required = false) String staffCode,
                                                        @RequestParam(required = false) String myStaffCode) {

        IPage<knowMainEx> res = iKnowMainService.getAll(pageNum,pageSize,knowTitle,staffCode,knowCateId,readCount,praiseCount,commentCount,knowState,myStaffCode);
        MExKnowMain9_2_SN_KCN resMessage = new MExKnowMain9_2_SN_KCN();
        resMessage.setList(res);
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }



    //查
    @GetMapping("getByKnowId")
    public ResponseEntity<MExKnowMain9_2_SN_KCNSingle> getByKnowId(@RequestParam Integer knowId) {

        knowMainExDaBiao res = iKnowMainService.getByKnowId(knowId);
        MExKnowMain9_2_SN_KCNSingle resMessage = new MExKnowMain9_2_SN_KCNSingle();
        resMessage.setItem(res);
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }



    //查
    @GetMapping("getAuditsKM")
    public ResponseEntity<MExKnowMain9_2_SN_KCN> getAuditsKM(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
                                                        @RequestParam(required = false, defaultValue = "10") Integer pageSize,
                                                        @RequestParam(required = false) String knowTitle,
                                                        @RequestParam(required = false) String auditUser,
                                                        @RequestParam(required = false) Integer knowState) {
        MExKnowMain9_2_SN_KCN resMessage = new MExKnowMain9_2_SN_KCN();
        archDept MDept = iArchDeptService.isManager(auditUser);
        if(MDept ==null){
            resMessage.setCode(100);
            resMessage.setTip("该用户不是部门主管！");
            return ResponseEntity.badRequest().body(resMessage);
        }
        List<staffList> SL=iStaffListService.listByDeptCode(MDept.getArchDeptCode());

        IPage<knowMainEx> res = iKnowMainService.getAuditsKM(pageNum,pageSize,knowTitle,auditUser,knowState,SL);

        resMessage.setList(res);
        resMessage.setCode(200);
        resMessage.setTip("查询成功");
        return ResponseEntity.ok(resMessage);

    }

    //增
    @PostMapping("Insert")
    public ResponseEntity<MessageBackKnowMainId> Insert(@RequestParam(required = false) String knowTitle,
                                          @RequestParam(required = false) String knowContent,
                                          @RequestParam(required = false) Integer knowCateId,
                                          @RequestParam String staffCode,
                                          @RequestParam(required = false) String auditUser,
                                          @RequestParam Integer type,
                                          @RequestParam(required = false) List<MultipartFile> files) {

        MessageBackKnowMainId res = iKnowMainService.Insert(knowTitle,knowContent,knowCateId,staffCode,auditUser);
        if(type==0){
            oBknowLabelMainService.insertFromKnowMainInsert(res.getKnowId(),staffCode);
        }
        else{
            String staffName = iStaffListService.getStaffName(staffCode);
            oBarchLabelMainService.insertFromKnowMainInsert(res.getKnowId(),staffName);
        }
//        System.out.println(files.size());
        if (files == null || files.size()==0) {
            // 没有传入文件
//            res.setTip(res.getTip()+",无附件");
        } else {
            // 有传入文件
//            res.setTip(res.getTip()+",有附件");
            for(MultipartFile item:files){
                iknowArchFilesService.Upload(item, res.getKnowId(), staffCode);
            }
        }
        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"新增了一个知识信息";
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //

        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }
//
//    //增
//    @GetMapping("InsertWithFiles")
//    public ResponseEntity<MessageBackKnowMainId> InsertWithFiles(@RequestParam(required = false) String knowTitle,
//                                                        @RequestParam(required = false) String knowContent,
//                                                        @RequestParam(required = false) Integer knowCateId,
//                                                        @RequestParam(required = false) String staffCode,
//                                                        @RequestParam(required = false) String auditUser) {
//
//
//
//
//        MessageBackKnowMainId res = iKnowMainService.Insert(knowTitle,knowContent,knowCateId,staffCode,auditUser);
//        if (res.getCode() == 200) {
//            return ResponseEntity.ok(res);
//        } else {
//            return ResponseEntity.badRequest().body(res);
//        }
//    }
//
    @PostMapping("Update")
    public ResponseEntity<Message> update(@RequestParam Integer knowId,
                                          @RequestParam(required = false) String knowTitle,
                                          @RequestParam(required = false) String knowContent,
                                          @RequestParam(required = false) Integer knowCateId,
//                                          @RequestParam(required = false) String staffCode,
                                          @RequestParam(required = false) String auditUser,
                                          @RequestParam(required = false) Integer knowState,
                                          @RequestParam(required = false) Integer readCount,
                                          @RequestParam(required = false) Integer praiseCount,
                                          @RequestParam(required = false) Integer commentCount) {


        System.out.println(1);
        if(knowContent!=null){
            Integer ContentSize = knowContent.length();
            if (ContentSize>5000){
                Message passL = new Message();
                passL.setCode(100);
                passL.setTip("内容长度为"+ContentSize+",过长！");
            }
        }
        //获取对应知识信息，用于下述message
        Map<Integer, knowMain> KMMap = obKnowMainService.getMapAll();
        knowMain OlditemForMes = KMMap.get(knowId);
        //
        //知识状态修改 打标完成-待审核 加入message表
        if(knowState != null && knowState == 5 && OlditemForMes.getKnowState() == 4){
            //operator改为部门主管的工号
            //根据知识的staffCode到staffList找对应部门，再到archDept找部门主管
            Map<String, staffList> SLMap = iStaffListService.getAll();
            Map<String, archDept> ADMap = iArchDeptService.getMapAll();

            String KMstaffCode = OlditemForMes.getStaffCode();
            String operator = ADMap.get(SLMap.get(KMstaffCode).getArchDeptCode()).getManager();
            String Content = KMstaffCode+"发布的知识\""+ OlditemForMes.getKnowTitle() + "\"待审核";
            iMessageService.insert(operator,Content);
        }
        //

        //知识状态修改 审核完成 加入message表
        if(knowState != null && knowState != 5 && OlditemForMes.getKnowState() == 5 && auditUser != null){
            String operator = OlditemForMes.getStaffCode();
            String Content ="知识\"" +OlditemForMes.getKnowTitle() + "\"已经被"+auditUser+"主管审核";
            iMessageService.insert(operator,Content);
        }
        //

        Message res = iKnowMainService.updateExceptSC(knowId,knowTitle,knowContent,knowCateId,auditUser,knowState,readCount,praiseCount,commentCount);
        //log4
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"更新了一个知识信息，知识id为："+knowId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
        //





        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }
//
//
    //9.1根据单个Id执行删除
    @GetMapping("DeleteByknowId")
    public ResponseEntity<Message> DeleteByknowId(@RequestParam Integer knowId) {

        Message mes  = iKnowMainService.deleteByknowId(knowId);
        //log4
//        if((operator!=null && !operator.isEmpty()) && (ipAddress!=null && !ipAddress.isEmpty())){
//            String content = operator+"删除了一个知识信息，知识id为："+knowId;
//            iLog4operateService.insertSingle(operator,content,ipAddress);
//        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"删除了一个知识信息，知识id为："+knowId;
        iLog4operateService.insertSingle(operator,content,ipAddress);
//        Log4Util.recordOperateLog("删除了一个知识信息，知识id为："+knowId);
        //

        if (mes.getCode() == 200) {
            return ResponseEntity.ok(mes);
        } else {
            return ResponseEntity.badRequest().body(mes);
        }
    }


    //知识的形式审查
    @GetMapping("Check")
    public ResponseEntity<Message> Check(@RequestParam Integer knowId) {

        Message res = iKnowMainService.check(knowId);
        //log4
//        if((operator!=null && !operator.isEmpty()) && (ipAddress!=null && !ipAddress.isEmpty())){
//            String content = operator+"对一条知识进行了审查，知识id为："+knowId;
//            iLog4operateService.insertSingle(operator,content,ipAddress);
//        }
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request1 = attributes.getRequest();
        String ipAddress=request1.getHeader("ip");
        String operator=request1.getHeader("staffCode");
        String content = operator+"对一条知识进行了审查，知识id为："+knowId;
        iLog4operateService.insertSingle(operator,content,ipAddress);

        //
        if (res.getCode() == 200) {
            return ResponseEntity.ok(res);
        } else {
            return ResponseEntity.badRequest().body(res);
        }
    }
//

}
